import {
  getLabelList
} from '../../apis/group';
const app = getApp();

Page({
  data: {
    labelList: [],
    inputValue: '',
    addLabelList: [],
    inputValue: ''
  },

  onLoad(options) {
    if (options.list) {
      this.setData({
        addLabelList: JSON.parse(options.list)
      });
    };

    this.getLabelList();
  },

  // 获取标签
  getLabelList() {
    getLabelList().then(res => {
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        data = data.map(v => {
          v.labelList = v.labelList.map(val => {
            return {
              ...val,
              select: false
            }
          });
          return {
            ...v,
            open: false
          }
        });
        this.setData({
          labelList: data
        });

        if (this.data.addLabelList.length !== 0) {
          this.data.addLabelList.forEach(v => {
            this.data.labelList.forEach((val, i) => {
              val.labelList.forEach((value, j) => {
                if (v === value.labelName) {
                  this.setData({
                    [`labelList[${i}].open`]: true,
                    [`labelList[${i}].labelList[${j}].select`]: true
                  });
                };
              });
            });
          });
        };
      };
    });
  },

  // 展开关闭列表
  openList(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      [`labelList[${index}].open`]: !this.data.labelList[index].open
    });
  },

  handleInput(e) {
    let {
      value
    } = e.detail;
    this.setData({
      inputValue: value
    });
  },

  // 输入添加
  addLabel() {
    if (this.data.addLabelList.length >= 5) {
      wx.showToast({
        title: '最多添加5个标签',
        icon: 'none'
      });
      return
    } else if (this.data.addLabelList.length < 5 && this.data.inputValue === '') {
      wx.showToast({
        title: '请输入内容...',
        icon: 'none'
      });
      return
    };
    this.data.addLabelList.push(this.data.inputValue);
    this.setData({
      addLabelList: this.data.addLabelList,
      inputValue: ''
    });
  },

  // 删除标签
  deleteLabel(e) {
    let {
      index,
      label
    } = e.currentTarget.dataset,
      list = this.data.addLabelList;

    this.data.labelList.forEach((val, i) => {
      val.labelList.forEach((value, j) => {
        if (label === value.labelName) {
          this.setData({
            [`labelList[${i}].labelList[${j}].select`]: false
          });
        };
      });
    });

    list.splice(index, 1);
    this.setData({
      addLabelList: list
    });
  },

  // 点击标签选择
  selectLabel(e) {
    let {
      index,
      iindex,
      label
    } = e.currentTarget.dataset;
    if (this.data.labelList[index].labelList[iindex].select) {
      this.data.addLabelList.splice(this.data.addLabelList.findIndex(v => v === label), 1);
    } else {
      if (this.data.addLabelList.length >= 5) {
        wx.showToast({
          title: '最多添加5个标签',
          icon: 'none'
        });
        return
      };
      this.data.addLabelList.push(label);
    };
    this.setData({
      [`labelList[${index}].labelList[${iindex}].select`]: !this.data.labelList[index].labelList[iindex].select,
      addLabelList: this.data.addLabelList
    });
  },

  // 完成
  confirmLabel() {
    if (this.data.addLabelList.length > 5) {
      wx.showToast({
        title: '最多添加5个标签',
        icon: 'none'
      });
      return
    };
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      label: this.data.addLabelList
    });
    wx.navigateBack({
      delta: 1
    });
  }
})